package cn.tedu.straw.portal.controller;


import cn.tedu.straw.portal.dto.StudentRegisterDTO;

import cn.tedu.straw.portal.ex.ParamValidationException;
import cn.tedu.straw.portal.service.IUserService;
import cn.tedu.straw.portal.util.Result;
import cn.tedu.straw.portal.vo.TeacherVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tedu.cn
 * @since 2020-11-12
 */
@RestController
@RequestMapping("/portal/user")
public class UserController {
    @Autowired
    IUserService userService;

    @RequestMapping("/reg/student")
    public Result studentRegister(@Valid StudentRegisterDTO studentRegisterDTO,
                                  BindingResult bindingResult){

        if(bindingResult.hasErrors()){
            String errorMessages= bindingResult.getFieldError().getDefaultMessage();
            throw new ParamValidationException(errorMessages);
        }
        userService.studentRegister(studentRegisterDTO);
        return Result.ok();
    }

    @GetMapping("/teachers")
    public Result getTeacherList(){
        List<TeacherVO> voList=userService.getAllTeachers();
        return Result.ok(voList);
    }


}
